package cn.com.kh;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import cn.com.elite.Util;

import  cn.com.kh.hst.ThreeDesUtil;


public class HST_test {

	public static void main(String[] args) throws Exception {

		
		String signfront = "";
		String signafter = "";
		String appid = "40001";
		String appsecret = "zf7HxnNBg2o8fnCf611";
	//	String managerId="120";
		String hsNo="1290598797";
		String pageNo="1";
		String pageSize="10";
		String token="26c4a5729c0c4f239076315f5ecc5eb1-648";
		String remoteAddr="192.168.66.74";
		long signtime=Instant.now().getEpochSecond();
		String sign_time=String.valueOf(signtime);
		System.out.println("sign_time: " + sign_time);
		//String managerId="123";
		HashMap<String, String> paramHashMap = new HashMap<String, String>() {
			{
				// put("managerId", managerId);
					  put("token", token);
                      put("hsNo", hsNo);
                  //    put("managerId", managerId);
                  //    put("pageNo", pageNo);
                  //    put("pageSize", pageSize);
                      put("remoteAddr", remoteAddr);
                      put("sign_time", sign_time);
			}
		};
		
		System.out.println("paramHashMap：" + paramHashMap);
		//自然排序 升序参数
		signfront= ThreeDesUtil.sortMap(paramHashMap,appid,appsecret);
		System.out.println("sign加密MD5前：" + signfront);
		signafter = Util.getMD5Str(signfront);
		System.out.println("sign加密MD5后：" + signafter);

		paramHashMap.put("client_key", appid);
		paramHashMap.put("sign", signafter);
		
		String injson=ThreeDesUtil.SplicingMap(paramHashMap,"&");
		System.out.println(injson);

	}
	
	//String getParam = SignUtil.getParam(apipath, receviceMap, secret, "\n", "sign");
	public static String getParamNVP(String headParam, Map<String, String> paramMap, String endParam, String pairsFlag) throws Exception {
		System.out.println("开始拼接参数NVP信息...");

		StringBuilder paramNVPBuilder = new StringBuilder();

		// 添加参数字符串头部
		if (endParam != null || endParam.length() > 0) {
			paramNVPBuilder.append(headParam);
		}

		// 对入参Map排序并拼接
		List<Map.Entry<String, String>> lstEntry = new ArrayList<>(paramMap.entrySet());
		Collections.sort(lstEntry, ((o1, o2) -> {
			return o1.getKey().compareTo(o2.getKey());
		}));
		lstEntry.forEach(o -> {
			System.out.println("NVP参数信息-" + o.getKey() + ":" + o.getValue());
			if (paramNVPBuilder == null || paramNVPBuilder.length() <= 0) {
				paramNVPBuilder.append(o.getKey()).append("=").append(o.getValue());
			} else {
				paramNVPBuilder.append(pairsFlag).append(o.getKey()).append("=").append(o.getValue());
			}
		});

		// 添加参数字符串尾部
		if (endParam != null || endParam.length() > 0) {
			paramNVPBuilder.append(pairsFlag).append(endParam);
		}

		System.out.println("返回拼接ParamNVP值:" + paramNVPBuilder.toString());

		return paramNVPBuilder.toString();

	}

	private static boolean equals(String string, String k) {
		// TODO Auto-generated method stub
		return false;
	}
	
	// encode加密
	public static String Getencode(String getKey) {
		String Rev_Value = null;
		if (getKey == null) {
			Rev_Value = "";
		} else {
			try {
				Rev_Value = URLEncoder.encode(getKey, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				Rev_Value = "";
			}
		}
		return Rev_Value;
	}
	// 非空校验isNull
	public static boolean isNull(String value) {

		boolean reFlag = true;
		if (value == "" || value.length() <= 0) {
			reFlag = true;
		} else {
			reFlag = false;
		}
		return reFlag;
	}

	// 解析Json值
	public static String GetJV(JSONObject inJson, String getKey) {
		String Rev_Value = null;

		try {
			Rev_Value = String.valueOf(inJson.getBoolean(getKey));
		} catch (Exception e6) {
			try {
				Rev_Value = String.valueOf(inJson.getDouble(getKey));
			} catch (Exception e5) {
				try {
					Rev_Value = inJson.getString(getKey);
				} catch (Exception e4) {
					try {
						Rev_Value = String.valueOf(inJson.getLong(getKey));
					} catch (Exception e11) {
						try {
							Rev_Value = String.valueOf(inJson.getInt(getKey));
						} catch (Exception e1) {
							try {
								Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
							} catch (Exception e2) {
								try {
									Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
								} catch (Exception e3) {
									Rev_Value = "";
								}
							}
						}
					}
				}
			}
		} finally {
			if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
				Rev_Value = "";
			}
		}
		return Rev_Value;
	}

	/**
	 * 坐标转换，腾讯地图转换成百度地图坐标
	 * 
	 * @param a 腾讯纬度
	 * @param b 腾讯经度
	 * @return 返回结果：经度,纬度
	 */
	public static String map_tx2bd(String a, String b) {
		double bd_lat;
		double bd_lon;
		double x_pi = 3.14159265358979324;
		// double x = b, y = a;

		double x = Double.valueOf(a);
		double y = Double.valueOf(b);
		double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
		double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
		bd_lon = z * Math.cos(theta) + 0.0065;
		bd_lat = z * Math.sin(theta) + 0.006;

		System.out.println("bd_lat:" + bd_lat);
		System.out.println("bd_lon:" + bd_lon);
		return bd_lon + "," + bd_lat;
	}

	public static byte[] SHA(final String strText) {
		// 是否是有效字符串
		if (strText != null && strText.length() > 0) {
			try {
				// SHA 加密开始
				// 创建加密对象 并傳入加密類型
				MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
				// 传入要加密的字符串
				messageDigest.update(strText.getBytes());
				// 得到 byte 類型结果
				return messageDigest.digest();

			} catch (NoSuchAlgorithmException e) {
				return null;
			}
		} else {
			return null;
		}
	}

	public static String checkTimeIsBetween(String timeString) throws ParseException {
		String format = "HH:mm:ss";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);

		Date timeDate1 = simpleDateFormat.parse(timeString);

		Date startTime = simpleDateFormat.parse("09:00:00");
		Date endTime = simpleDateFormat.parse("21:00:00");

		String dateresult;
		if (timeDate1.before(startTime) || timeDate1.after(endTime)) {// 此时间不 在当天9点至21点之间
			dateresult = "0";
		} else {// 此时间在当天9点至21点之间
			dateresult = "1";
		}
		return dateresult;
	}

	public static String HTMLDecode(String str) {

		String s = "";
		if (str.length() == 0) {
			return "";
		} else {
			s = str.replaceAll("\r\n", "");
			s = s.replaceAll("\'", "\"");
			s = s.replaceAll("\"", "\"");
			return s;
		}

	}

}
